var gulp = require('gulp');
var less = require('gulp-less');
var path = require('path');
var babel = require("gulp-babel");
var concat = require('gulp-concat');
var sourcemaps = require('gulp-sourcemaps');
var exec = require('child_process').exec;
var uglify = require('gulp-uglifyjs');
var cleanCSS = require('gulp-clean-css');
var gutil = require('gulp-util');


gulp.task('test',['default'],function() {
  exec('"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe" http://localhost:9090/wordpress/',function(err){
    if(err)console.log(err);
  })
})

gulp.task('default',['styles','scripts','watch'], function() {
});

gulp.task('styles',function(){
  return gulp.src('./less/style.less')
    .pipe(sourcemaps.init())
    .pipe(less())
    .on('error', function(err) {
        gutil.log('Less Error!', err.message);
        this.end();
    })
    .pipe(cleanCSS({compatibility: 'ie8'}))
    .pipe(sourcemaps.write("."))
    .pipe(gulp.dest('./'))
    .pipe(gulp.dest('./dist/css/'));
})

gulp.task("scripts", function () {
  return gulp.src(["lib/bootstrap/dist/js/bootstrap.js","src/**/*.js"])
    .pipe(sourcemaps.init())
    .pipe(babel())
    .pipe(uglify())
    .pipe(concat("all.js"))
    .pipe(sourcemaps.write("."))
    .pipe(gulp.dest("dist/js"));
});

gulp.task('watch',function(){
 gulp.watch('./less/**/*.less',['styles']);
 gulp.watch('src/**/*.js',['scripts']);
});